<template>
  <div class="chatbot">
    <!-- 聊天框 -->
    <el-scrollbar height="68vh" ref="scrollbarRef" always>
      <div class="messages" ref="innerRef">
        <div
          v-for="(message, index) in messages"
          :key="index"
          class="msg"
          :class="{
            'user-msg': index % 2 === 0,
            'response-msg': index % 2 === 1,
          }"
        >
          <div class="botIcon" v-if="index % 2 === 1">
            <img src="@/assets/catbot.png" />
          </div>
          <div class="message">{{ message }}</div>
          <div class="useIcon" v-if="index % 2 === 0">
            <img src="@/assets/catbot.png" />
          </div>
        </div>
      </div>
    </el-scrollbar>
    <!-- 输入框 -->
    <div class="user-input">
      <el-input
        v-model="prompt"
        @keyup.enter="generateResponse"
        placeholder="请输入你的问题"
        size="large"
        maxlength="500"
        show-word-limit
        clearable
        autosize
        :disabled="isLoading"
        autofocus
      />
      <el-button type="primary" @click="generateResponse" :disabled="isLoading"
        >发送</el-button
      >
      <el-button
        type="primary"
        circle
        @click="clearmsg"
        :disabled="isLoading"
        :icon="Delete"
      />
    </div>
  </div>
</template>

<script>
import { fetchCompletions } from "../api.js";
import { Delete } from "@element-plus/icons-vue";

export default {
  data() {
    return {
      messages: [],
      prompt: "",
      isLoading: false,
      innerRef: null,
      scrollbarRef: null,
      Delete,
    };
  },
  methods: {
    async generateResponse() {
      this.isLoading = true;
      this.$nextTick(() => {
        if (this.innerRef && this.innerRef.clientHeight > 200) {
          this.scrollbarRef.setScrollTop(this.innerRef.clientHeight);
        }
      });
      try {
        if (this.prompt.trim() !== "") {
          this.messages.push(this.prompt);

          const response = await fetchCompletions(this.prompt);
          if (response) {
            // console.log(response);
            console.log(response.data.Data.Text);
            this.messages.push(response.data.Data.Text);
          }
          this.prompt = "";
        }
      } catch (error) {
        console.log("执行错误", error);
      } finally {
        this.isLoading = false;
        this.$nextTick(() => {
          if (this.innerRef && this.innerRef.clientHeight > 200) {
            this.scrollbarRef.setScrollTop(this.innerRef.clientHeight);
          }
        });
      }
    },
    clearmsg() {
      this.messages = [];
      this.prompt = "";
    },
  },
  mounted() {
    this.innerRef = this.$refs.innerRef;
    this.scrollbarRef = this.$refs.scrollbarRef;
  },
};
</script>

<style scoped>
/* 聊天框样式 */
.chatbot {
  display: flex;
  flex-direction: column;
  max-width: 800px;
  margin: 0 auto;
}

.messages {
  flex-grow: 1;
  list-style-type: none;
  overflow-y: auto;
  /* height: 68vh; */
  margin: 0;
  padding: 0;
}

/* 每一条消息 */
.msg {
  display: flex;
}

/* 消息框的样式 */
.message {
  padding: 1rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
  margin-bottom: 1rem;
  line-height: 1.5;
  letter-spacing: 2px;
  flex: 1;
}

/* 下面是机器人和用户的头像样式 */
.botIcon {
  width: 50px;
  height: 50px;
  margin-right: 10px;
}

.botIcon img {
  width: 100%;
}

.useIcon {
  width: 50px;
  height: 50px;
  margin-left: 10px;
}

.useIcon img {
  width: 100%;
}

.user-msg {
  text-align: right;
  color: #2d8ff7;
}

.response-msg {
  text-align: left;
  color: rgb(243, 56, 115);
}
/* 用户的输入框 */
.user-input {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: fixed;
  bottom: 5%;
  left: 15%;
  width: 70%;
  padding: 10px;
  z-index: 999;
}

input {
  flex: 1;
  padding: 1rem;
}
/* 按钮格式 */
button {
  padding: 1rem;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 0.5rem;
  cursor: pointer;
  margin-left: 1rem;
}
</style>
